SPB Git

Repository Public

route.ts 4.0 KB 82 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/app/api/cost/assemblies/[code]/route.ts9 * Module           : app10 * Repository       : https://git.spboucher.ai/vrai-prix11 * Created          : 2026-09-0812 * Last Modified    : 2026-09-0813 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Vrai-Prix — méthode du coût : fiche d'un assemblage (composition, prix, main-d'œuvre, sources, variations régionales, historique).43import { NextRequest, NextResponse } from "next/server";44import { buildContext, loadLocations } from "@/lib/cost/catalog";45import { assemblyUnitCost } from "@/lib/cost/engine";46import { cached } from "@/lib/cost/cache";4748export const runtime = "nodejs";4950export async function GET(req: NextRequest, { params }: { params: Promise<{ code: string }> }) {51  const { code: raw } = await params;52  const code = decodeURIComponent(raw);53  const loc = req.nextUrl.searchParams.get("location") ?? "QC-MTL";54  try {55    const data = cached(`assembly:${code}:${loc}`, 10 * 60_000, () => {56      const ctx = buildContext({ locationCode: loc });57      const a = ctx.assemblies.get(code);58      if (!a) return null;59      const unit = assemblyUnitCost(a, ctx);60      // variations régionales (facteurs appliqués au coût direct par unité)61      const regional = loadLocations().map((l) => ({ code: l.code, nameFr: l.nameFr, nameEn: l.nameEn, direct: Math.round(100 * (unit.material * l.materialFactor + unit.labour * l.labourFactor + unit.equipment * l.equipmentFactor)) / 100, materialFactor: l.materialFactor, labourFactor: l.labourFactor, equipmentFactor: l.equipmentFactor }));62      // historique : coût unitaire recalculé aux 8 derniers trimestres avec les prix de l'époque63      const history: { date: string; direct: number; material: number; labour: number; equipment: number; kind: string }[] = [];64      const now = new Date(ctx.asOf);65      for (let k = 7; k >= 0; k--) {66        const d = new Date(now);67        d.setMonth(d.getMonth() - 3 * k);68        const asOf = d.toISOString().slice(0, 10);69        const c2 = buildContext({ asOf, locationCode: loc });70        const u2 = assemblyUnitCost(a, c2);71        const kinds = new Set(u2.components.map((c) => c.provenance.kind));72        history.push({ date: asOf, direct: u2.direct, material: u2.material, labour: u2.labour, equipment: u2.equipment, kind: kinds.has("observed") || kinds.has("official") ? "observed" : kinds.has("indexed") ? "indexed" : "reference" });73      }74      return { asOf: ctx.asOf, location: ctx.location, assembly: a, unit, regional, history };75    });76    if (!data) return NextResponse.json({ error: "assemblage introuvable" }, { status: 404 });77    return NextResponse.json(data);78  } catch (e) {79    return NextResponse.json({ error: (e as Error).message }, { status: 500 });80  }81}82
Files main
Menu

Theme